#include <Arduino.h>
#include <U8g2lib.h>



#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif

/*
  U8g2lib Example Overview:
    Frame Buffer Examples: clearBuffer/sendBuffer. Fast, but may not work with all Arduino boards because of RAM consumption
    Page Buffer Examples: firstPage/nextPage. Less RAM usage, should work with all Arduino boards.
    U8x8 Text Only Example: No RAM usage, direct communication with display controller. No graphics, 8x8 Text only.
    
*/


U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ 16, /* data=*/ 17, /* reset=*/ U8X8_PIN_NONE);   // ESP32 Thing, pure SW emulated I2C



// End of constructor list


int gas_din=2;
int gas_ain=A0;
int ad_value;

void setup(void) {
  u8g2.begin();
  pinMode(gas_din,INPUT);
  pinMode(gas_ain,INPUT);
  Serial.begin(115200);
}

void loop(void) {
  ad_value=analogRead(gas_ain);
  if(digitalRead(gas_din)==LOW)
  {
    Serial.println("Gas leakage");
    Serial.print("ad_value:");
    Serial.print(ad_value*3.3/1024);
    Serial.println("V");

    u8g2.clearBuffer();          // clear the internal memory
    u8g2.setFont(u8g2_font_ncenB08_tr); // choose a suitable font
    char t[] = "Gas Value:";
    char v[5];
    dtostrf(ad_value*3.3/1024,1,2,v);
    strcat(t,v);
    
    u8g2.drawStr(10,20,"Found Alcohol!!!");  // write something to the internal memory
    u8g2.drawStr(10,45,t);  // write something to the internal memory
    u8g2.sendBuffer();          // transfer internal memory to the display
    delay(1000);  
  }
  else
  {
    Serial.println("Gas not leak");
    
    u8g2.clearBuffer();          // clear the internal memory
    u8g2.setFont(u8g2_font_ncenB08_tr); // choose a suitable font
    u8g2.drawStr(10,38,"Not Found Alcohol!!!");  // write something to the internal memory
    u8g2.sendBuffer();          // transfer internal memory to the display
    delay(1000);  
  }

}
